package com.cykj.pos.controller.api;


import com.cykj.common.core.domain.AjaxResult;
import com.cykj.pos.domain.BizMerchantEntry;
import com.cykj.pos.domain.BizPosMachine;
import com.cykj.pos.mapper.BizPosMachineMapper;
import com.cykj.pos.service.IBizMerchantEntryService;
import com.cykj.pos.service.IBizPosMachineService;
import com.cykj.pos.service.IBizPosMachineStatusRecordsService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * pos利盟伙伴 设备入库  报件同步
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/synchronization")
public class PosV2SynchronizationController {

    private final BizPosMachineMapper posMachineMapper;

    private final IBizMerchantEntryService iBizMerchantEntryService;

    private final IBizPosMachineStatusRecordsService recordsService;

    private final IBizPosMachineService bizPosMachineService;

    /**
     * 同步设备入库
     *
     * @return
     */
    @PostMapping("/lmhb/sameToQuote")
    public Map sameToQuote(@RequestBody BizPosMachine bizPosMachine) {
        Map<String, String> map = new HashMap();
        System.out.println("------------------------");
        System.out.println("接收报文---" + bizPosMachine);
        int row = posMachineMapper.insertPosMachine(bizPosMachine);
        if (row > 1) {
            map.put("ret_code", "0");
            map.put("ret_msg", "同步成功");
            return map;
        } else {
            map.put("ret_code", "1");
            map.put("ret_msg", "同步失败");
            return map;
        }
    }

    @ApiOperation("同步商户报件")
    @PostMapping("/lmhb/samePosEntry")
    public Map samePosEntry(@RequestBody BizMerchantEntry bizMerchantEntry) {
      // 1 设备表激活  2激活表插激活数据  运行函数
        System.out.println("--------------同步商户入件---------");
        Boolean row = false;
        Map<String, String> map = new HashMap();
        System.out.println("------------------------");
        System.out.println("接收报文---" + bizMerchantEntry);
        //添加商户入件信息
        bizMerchantEntry.setId(null);
        row = iBizMerchantEntryService.save(bizMerchantEntry);
       // BizMerchantEntry bizMerchantEntryByMerchantNo = iBizMerchantEntryService.getBizMerchantEntryByMerchantNo(bizMerchantEntry.getMerchantNo());
        bizMerchantEntry.setId(bizMerchantEntry.getId());
        iBizMerchantEntryService.insertBizEntrySn(bizMerchantEntry);
        //激活表填数据
        int ok = recordsService.snRecordsOneVersion(bizMerchantEntry);
        if (ok>0){
            map.put("ret_code", "0");
            map.put("ret_msg", "同步成功");
            return map;
        }else {
            map.put("ret_code", "1");
            map.put("ret_msg", "同步失败");
            return map;
        }

    }
    @ApiOperation("同步商户报件审核状态")
    @PostMapping("/lmhb/samePosIsAudit")
    public Map samePosIsAudit(@RequestBody BizMerchantEntry bizMerchantEntry) {
      // 1 设备表激活  2激活表插激活数据  运行函数
        Boolean row = false;
        Map<String, String> map = new HashMap();
        System.out.println("------------------------");
        System.out.println("接收报文---" + bizMerchantEntry);
        String merchantNo = bizMerchantEntry.getMerchantNo();
        String proMerSub = bizMerchantEntry.getAgentid();
        String snCode = bizMerchantEntry.getSn();
        //更新商户入件状态
        iBizMerchantEntryService.updateMerchantEntry(merchantNo, proMerSub);
        //更新激活表状态
        recordsService.updatePosRecordsStatus(snCode);
        //更新设备表
        bizPosMachineService.updatePosPosMachine(snCode);
        map.put("ret_code", "0");
        map.put("ret_msg", "同步成功");
        return map;

    }

}
